package com.hspedu.homework;


/**
 * @Author Stringzhua
 * @Date 2024/3/7 14:46
 * description:
 * 1.计算器接口有一个work()方法,功能是运算
 * 有一个手机类Cellphone,定义方法testWork测试计算功能,调用计算接口的work()方法
 * 2.要求调用Cellphone对象的testWork方法,使用上匿名内部类
 */
public class homework04 {
    public static void main(String[] args) {
        Cellphone cellphone = new Cellphone();
        /* 1.匿名内部类是 同时也是一个对象 编译类型是ICalculate 匿名内部类
        (new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return 0;
            }
        });
        * */
        cellphone.testWork(new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return n1 + n2;
            }
        }, 10, 8);//18.0

        cellphone.testWork(new ICalculate() {
            @Override
            public double work(double n1, double n2) {
                return n1 * n2;
            }
        }, 10, 8);//80
    }
}

interface ICalculate {
    public double work(double n1, double n2);//该方法完成怎样的计算,在匿名内部类去完成
}

class Cellphone {
    //当我们调用testWork方法时,直接传入一个实现了iCalculate接口的匿名内部类即可
    //该匿名内部类,可以灵活的实现,完成不同的计算
    public void testWork(ICalculate iCalculate, double n1, double n2) {
        double result = iCalculate.work(n1, n2);//动态绑定机制
        System.out.println("计算后的结果是" + result);
    }
}
